package com.yueze.demo1.controller;

import com.yueze.demo1.entities.User;
import com.yueze.demo1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * @author Administrator
 * @create 2020-08-08
 * 13:51
 * @desc
 **/
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/user/register")
    public String register(){
        return "register";
    }

    @RequestMapping("/user/login")
    public String login(User user, HttpSession session){

        User u = userService.login(user);

        if (u != null){
            session.setAttribute("user",u);
           return "redirect:/index/select";
        }else {
            return "forword:/";
        }


    }


    @RequestMapping("/user/insert")
    public String insert(User user){

        System.out.println(user);
        User u = userService.login(user);

        if (u != null){
            return "redirect:/user/register";
        }else {
            userService.register(user);
        }

        return "redirect:/";
    }
}
